<?php
/**
 * @title 应用公共文件
 * @author Leruge
 * @email leruge@163.com
 * @qq 305530751
 */

use think\exception\HttpResponseException;

/**
 * @title 发送验证码
 *
 * @param string $phone 手机
 * @param string $code 验证码
 *
 * @return array 发送结果，code是1发送成功，msg是提示信息
 */
function send_code(string $phone, string $code)
{
    // 集成短信扩展
    return ['code' => 1, 'msg' => '发送成功'];
}

/**
 * @title 格式化时间
 *
 * @param int|null $time 整型时间戳
 * @param int $type 1是Y-m-d H:i:s；2是Y-m-d H:i；3是Y-m-d
 *
 * @return string 时间
 */
function format_time(?int $time, int $type = 1)
{
    if (!$time) {
        return null;
    }
    switch ($type) {
        case 1:
            $formatTime = date('Y-m-d H:i:s', $time);
            break;
        case 2:
            $formatTime = date('Y-m-d H:i', $time);
            break;
        case 3:
            $formatTime = date('Y-m-d', $time);
            break;
        default:
            $formatTime = date('Y-m-d H:i', $time);
    }
    return $formatTime;
}

/**
 * @title 递归组装树状接口
 *
 * @param array $data 原始数据
 * @param int $pid 从哪里开始组装
 *
 * @return array 新组装的数据
 */
function recursion(array $data, int $pid = 0)
{
    static $arr = [];
    foreach ($data as $k => $v) {
        if ($v['pid'] == $pid) {
            $arr[] = $v;
            recursion($data, $v['pid']);
        }
    }
    return $arr;
}

/**
 * @title 生成昵称
 *
 * @return string 昵称
 * @throws Exception
 */
function generate_nickname()
{
    return mb_strtoupper(bin2hex(random_bytes(3)));
}

/**
 * @title 获取图片
 *
 * @param string|null $pics 图片
 * @param int $type 1是单张需要默认；2是多张；3是单张不需要默认
 *
 * @return string|null 图片
 */
function handle_get_pic(?string $pics, int $type = 1)
{
    if ($type == 1) {
        $pic = $pics ?: '/static/default/no.jpg';
        if (substr($pic, 0, 1) == '/') {
            $pic = config('extra.app_url') . trim($pic, '/');
        }
        return $pic;
    } elseif ($type == 2) {
        $picArray = array_filter(explode(',', $pics));
        $newPicArray = [];
        foreach ($picArray as $v) {
            if (substr($v, 0, 1) == '/') {
                $newPicArray[] = config('extra.app_url') . trim($v, '/');
            } else {
                $newPicArray[] = $v;
            }
        }
        return implode(',', $newPicArray);
    } elseif ($type == 3) {
        if ($pics) {
            if (substr($pics, 0, 1) == '/') {
                $pics = config('extra.app_url') . trim($pics, '/');
            }
            return $pics;
        } else {
            return null;
        }
    } else {
        return null;
    }
}

/**
 * @title 设置图片
 *
 * @param string $pics 图片
 * @param integer $type 1是单张；2是多张
 *
 * @return string 图片
 */
function handle_set_pic(string $pics, int $type = 1)
{
    if ($type == 1) {
        if (stripos($pics, config('extra.app_url')) === 0) {
            $handlePics = '/' . trim(str_ireplace(config('extra.app_url'), '/', $pics), '/');
        } else {
            $handlePics = $pics;
        }
        return $handlePics;
    } else {
        $picArray = array_filter(explode(',', $pics));
        foreach ($picArray as $k => $v) {
            if (stripos($v, config('extra.app_url')) === 0) {
                $handlePics = '/' . trim(str_ireplace(config('extra.app_url'), '/', $v), '/');
            } else {
                $handlePics = $v;
            }
            $picArray[$k] = $handlePics;
        }
        return implode(',', $picArray);
    }
}

/**
 * @title 处理内容
 *
 * @param string|null $content 内容
 *
 * @return string 内容
 */
function handle_set_content(?string $content = null)
{
    return str_ireplace('src="' . config('extra.app_url'), 'src="/', $content);
}

/**
 * @title 获取内容
 *
 * @param string|null $content 内容
 *
 * @return string 内容
 */
function handle_get_content(?string $content = null)
{
    return str_ireplace('src="/', 'src="' . config('extra.app_url'), $content);
}


/**
 * @title 获取数字
 *
 * @param float|null $num 数字
 *
 * @return string 数量
 */
function handle_get_num(?float $num = null)
{
    $num = $num ?: 0;
    if ($num > 9999) {
        $num = round($num / 10000, 1) . 'w';
    }
    return $num;
}

/**
 * @title 设置数字
 *
 * @param string|array $num 字符串数字
 *
 * @return float 数字
 */
function handle_set_num($num = 0)
{
    if (is_array($num)) {
        $num = $num[1] ?? 0;
    }
    if (stripos($num, 'w')) {
        $num = substr($num, 0, -1) * 10000;
    }
    return $num;
}

/**
 * @title 生成订单号
 *
 * @param int $type 暂无
 *
 * @return string 订单号
 */
function generate_no(int $type)
{
    $prefixArray = [
        1 => '',
    ];
    $suffixArray = [
        1 => '',
    ];
    $prefix = $prefixArray[$type] ?? 'no-';
    $suffix = $suffixArray[$type] ?? '';
    return $prefix . time() . mt_rand(1000, 9999) . $suffix;
}

/**
 * @title 生成私信房间号
 *
 * @param int $fromId 发送者ID
 * @param int $toId 接受者ID
 *
 * @return string 房间号
 */
function generate_room_num(int $fromId, int $toId)
{
    return $fromId > $toId ? $toId . '-' . $fromId : $fromId . '-' . $toId;
}

/**
 * @title 封装json响应
 *
 * @param array|null $data 数据
 * @param integer $code 1是成功；0是失败；其它码看说明
 * @param string $msg 提示消息
 * @param string|null $url url地址
 *
 * @return \think\response\Json 响应
 */
function result(?array $data, int $code, string $msg = '操作成功', ?string $url = null)
{
    $res = [
        'data' => $data,
        'code' => $code,
        'msg' => $msg,
        'url' => $url
    ];
    throw new HttpResponseException(json($res));
}
